package info.qingshui.browser.hk;

import android.content.Intent;
import android.net.Uri;
import android.webkit.DownloadListener;
import info.qingshui.browser.R;

public class HKDownloadListener implements DownloadListener {
	HKActivity hkActivity;
	
	public HKDownloadListener(HKActivity hkActivity){
		this.hkActivity = hkActivity;
	}
	
	public void onDownloadStart(String url, String userAgent,
			String contentDisposition, String mimetype,
			long contentLength) {
		//if( url.toLowerCase().endsWith(".apk") ) {
			//看成是安装包，直接下载安装
		//}
		
		if ( true ) {//TODO 暂不支持下载,丢到系统去下载吧
			Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, mimetype);
            hkActivity.startActivity(intent);
            

			//Toast.makeText(webView.getContext(), "当前版本暂不支持下载文件", Toast.LENGTH_LONG).show();
			return;
		} 
		/*
			//下面是下载安装程序
			InputStream is;
			try {
				URL u = new URL(url);
				HttpURLConnection con = (HttpURLConnection) u
						.openConnection();
				con.setRequestMethod("GET");
				con.setDoOutput(true);
				con.connect();
				is = con.getInputStream();
				// Path and File where to download the APK
				String path = Environment.getExternalStorageDirectory()
						+ "/apdroid/";
				String fileName = url.substring(url.lastIndexOf('/') + 1);
				File dir = new File(path);
				dir.mkdirs(); // creates the download directory if not exist
				File outputFile = new File(dir, fileName);
				FileOutputStream fos = new FileOutputStream(outputFile); 
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = is.read(buffer)) != -1) {
					fos.write(buffer, 0, len);
				}
				fos.close();
				is.close();
				Intent intent = new Intent(Intent.ACTION_VIEW);
				String name = Environment.getExternalStorageDirectory()
						+ "/apdroid/"
						+ url.substring(url.lastIndexOf('/') + 1);
				intent.setDataAndType(Uri.fromFile(new File(name)),
						"application/vnd.android.package-archive");
				//startActivity(intent);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}*/
	}
}
